package com.tdkj.action.backstage.yellowpage;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.tdkj.action.BaseAction;
import com.tdkj.model.friendlylink.BeanFriendlylink;
import com.tdkj.model.website.BeanProtocol;
import com.tdkj.model.website.BeanProtocolDetail;
import com.tdkj.model.yellowpage.BeanEnterprice;
import com.tdkj.service.yellowpage.IBeanEnterpriceService;
public class EnterpriceAction  extends BaseAction{
	private long id;
	private BeanEnterprice enterprice=new BeanEnterprice();;
	private IBeanEnterpriceService enterpriceService;
	private List<BeanEnterprice> enterprices=new ArrayList<BeanEnterprice>();
	private String[] ids;
	public String deleteAll() {
		if (null != ids) {
			for (String idtemp : ids) {
				this.getBaseService().delete(Long.parseLong(idtemp), BeanEnterprice.class);
			}
		}
		return findAll();
	}
	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public List<BeanEnterprice> getEnterprices() {
		return enterprices;
	}
	public void setEnterprices(List<BeanEnterprice> enterprices) {
		this.enterprices = enterprices;
	}
	public BeanEnterprice getEnterprice() {
		return enterprice;
	}
	public void setEnterprice(BeanEnterprice enterprice) {
		this.enterprice = enterprice;
	}
	public IBeanEnterpriceService getEnterpriceService() {
		return enterpriceService;
	}
	@Resource
	public void setEnterpriceService(IBeanEnterpriceService enterpriceService) {
		this.enterpriceService = enterpriceService;
	}
	//后去qq列表
	public String findAll(){
		return "findAll";
	}
	//后去qq列表
	public String findRightPage(){
		enterprices=enterpriceService.findAll(BeanEnterprice.class);
		if(enterprices.size()==0)appendObj(20);
		Criteria criteria = this.createCriteria(BeanEnterprice.class);
		criteria.addOrder(Order.desc("createtime"));// 对状态进行排序
		this.queryByPage(criteria, "enterpriceAction!findRightPage.action", 10);
		return "rightPage";
	}
	//后去qq列表
	public String findTree(){
		return "tree";
	}
	//获取添加界面
	public String findEditPage(){
		enterprice=enterpriceService.findById(this.getId(), BeanEnterprice.class);
		return "editPage";
	}
	//获取添加界面
	public String findAddPage(){
		return "addPage";
	}
	//删除QQ
	public String deleteEnterprice(){
		enterprice.setId(this.getId());
		enterpriceService.delete(enterprice);
		return findRightPage();
	}
	//获取添加界面
	public String addEnterprice(){
		enterprice.setCreatetime(new Timestamp(System.currentTimeMillis()));
		enterpriceService.save(enterprice);
		return findAll();
	}
	//获取添加界面
	public String editEnterprice(){
		enterpriceService.merge(enterprice);
		return findRightPage();
	}
	private  void appendObj(int num) {
		// TODO Auto-generated method stub
		BeanEnterprice bean=null;
		for(int i=0;i<num;i++){
			bean=new BeanEnterprice();
			bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
			bean.setEnterpricename("科技之星"+i);
			enterpriceService.save(bean);
		}
	}
	
}
